load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")

package(default_visibility = ["//visibility:private"])

licenses(["notice"])

exports_files(["COPYING"])

# To enable nghttp2 debug output, specify:
# bazel build --@nghttp2//:debug
bool_flag(
    name = "debug",
    build_setting_default = False,
    visibility = ["//visibility:public"],
)

config_setting(
    name = "debug_setting",
    flag_values = {
        ":debug": "True",
    },
)

cc_library(
    name = "nghttp2",
    hdrs = [
        "lib/includes/nghttp2/nghttp2.h",
        "lib/includes/nghttp2/nghttp2ver.h",
    ],
    defines = [
        "NGHTTP2_STATICLIB",
    ],
    strip_include_prefix = "lib/includes",
    visibility = ["//visibility:public"],
    deps = [
        ":nghttp2_impl",
    ],
)

cc_library(
    name = "nghttp2_impl",
    srcs = glob(["lib/*.c"]),
    hdrs = glob(["lib/*.h"]),
    copts = select({
        "@rules_cc//cc/compiler:msvc-cl": [],
        "//conditions:default": [
            "-Wno-string-plus-int",
        ],
    }),
    local_defines = [
        "BUILDING_NGHTTP2",
        "HAVE_CONFIG_H",
    ] + select(
        {
            ":debug_setting": ["DEBUGBUILD"],
            "//conditions:default": [],
        },
    ),
    deps = [
        ":nghttp2_config",
        ":nghttp2_headers",
    ],
)

cc_library(
    name = "nghttp2_headers",
    hdrs = [
        "lib/includes/nghttp2/nghttp2.h",
        "lib/includes/nghttp2/nghttp2ver.h",
    ],
    defines = [
        "NGHTTP2_STATICLIB",
    ],
    strip_include_prefix = "lib/includes",
)

cc_library(
    name = "nghttp2_config",
    hdrs = [
        "lib/config.h",
    ],
    strip_include_prefix = "lib",
)

expand_template(
    name = "nghttp2ver_h",
    out = "lib/includes/nghttp2/nghttp2ver.h",
    substitutions = {
        "@PACKAGE_VERSION@": "1.65.0",
        "@PACKAGE_VERSION_NUM@": "0x014100",
    },
    template = "lib/includes/nghttp2/nghttp2ver.h.in",
)

# Generated based on running configure on several platforms.
copy_file(
    name = "config_h",
    src = "config/config.h",
    out = "lib/config.h",
)
